package com.douqu.game.auth.web.controller;

import com.douqu.game.auth.service.PlayerService;
import com.douqu.game.auth.web.request.AuthReqDto;
import com.douqu.game.auth.web.request.LoginReqDto;
import com.douqu.game.auth.web.request.RegisterReqDto;
import com.douqu.game.auth.web.request.UpdateRecordReqDto;
import com.douqu.game.core.e.ReturnMessage;
import com.douqu.game.core.factory.HttpFactory;
import com.douqu.game.core.web.response.BaseResponseDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Author : wangzhenfei
 * 2017-07-06 15:55
 */
@RestController
public class PlayerController {

    @Autowired
    private PlayerService playerService;

    @RequestMapping(value = HttpFactory.PLAYER_AUTH, method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public BaseResponseDto auth(String account, String password, String ip, Integer channel,Integer serverRuleId, String deviceguid,String imei)
    {
        if (serverRuleId == null){
            return new BaseResponseDto(ReturnMessage.FAILURE);
        }
        AuthReqDto loginReqDto = new AuthReqDto();
        loginReqDto.setIp(ip == null ? "" : ip);
        loginReqDto.setDeviceId(account);
        loginReqDto.setAccount(account);
        loginReqDto.setChannel(channel == null ? 0 : channel.intValue());
        loginReqDto.setPassword(password);
        loginReqDto.setServerRuleId(serverRuleId);
        loginReqDto.setDeviceId(deviceguid);
        loginReqDto.setImei(imei);
        return playerService.auth(loginReqDto);
    }

//    @RequestMapping(value = "/bandPhoneNumGetCaptcha", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
//    public BaseResponseDto bandPhoneNumGetCaptcha(String deviceId, String phoneNum)
//    {
//        return playerService.bandPhoneNumGetCaptcha(deviceId, phoneNum);
//    }


    @RequestMapping(value = HttpFactory.PLAYER_BAND_PHONE_NUM, method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public BaseResponseDto bandPhoneNum(String deviceId, String phoneNum, String captcha)
    {
        return playerService.bandPhoneNum(deviceId, phoneNum, captcha);
    }

    @RequestMapping(value = HttpFactory.PLAYER_GET_CAPTCHA, method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public BaseResponseDto getCaptcha(String phone)
    {
        return  playerService.getCaptcha(phone);
    }


    /**
     * 前端调用
     * @param account
     * @param channel
     * @param serverRuleId
     * @return
     */
    @RequestMapping(value = HttpFactory.PLAYER_GET_SERVER_LIST, method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public BaseResponseDto getServerList(String account, Integer channel, Integer serverRuleId)
    {
        return  playerService.getServerList(account,channel,serverRuleId);
    }

    @RequestMapping(value = HttpFactory.PLAYER_REGISTER, method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public BaseResponseDto register(@RequestBody RegisterReqDto register)
    {
        return playerService.register(register);
    }



    @RequestMapping(value = HttpFactory.PLAYER_GET_PHONE_BAND_DEVICE, method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public BaseResponseDto getPhoneBandDevice(@RequestBody UpdateRecordReqDto updateRecordReqDto)
    {
        return playerService.getPhoneBandDevice(updateRecordReqDto.getAccount());
    }

    //***********************************************************************

    @RequestMapping(value = HttpFactory.PLAYER_UPDATE_RECORD, method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public BaseResponseDto updateRecord(@RequestBody UpdateRecordReqDto updateRecordReqDto)
    {
        return playerService.updateRecord(updateRecordReqDto);
    }

    @RequestMapping(value = HttpFactory.PLAYER_UPDATE_RECORD2, method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public BaseResponseDto updateRecord2(@RequestBody UpdateRecordReqDto updateRecordReqDto)
    {
        return playerService.updateRecord(updateRecordReqDto);
    }



    @RequestMapping(value = HttpFactory.PLAYER_MERGE_SERVER, method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public BaseResponseDto mergeServer(@RequestBody UpdateRecordReqDto updateRecordReqDto)
    {
        return playerService.mergeServer(updateRecordReqDto);
    }


    @RequestMapping(value = HttpFactory.PLAYER_LOGIN_SUCCESS, method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public BaseResponseDto loginSuccess(@RequestBody LoginReqDto loginReqDto,HttpServletRequest request, HttpServletResponse response)
    {
        return playerService.loginSuccess(loginReqDto,request,response);
    }

    @RequestMapping(value = HttpFactory.PLAYER_GET_WHITE_LIST,method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public BaseResponseDto getWhiteList(HttpServletRequest request,HttpServletResponse response){
        return playerService.getWhiteList();
    }
}
